package com.caiheng.api.service.titlemenu.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.caiheng.api.dao.estate.RepairRecordMapper;
import com.caiheng.api.dao.titlemenu.TitleMenuMapper;
import com.caiheng.api.entity.estate.RepairRecord;
import com.caiheng.api.entity.sys.SysUser;
import com.caiheng.api.entity.titlemenu.TitleMenu;
import com.caiheng.api.entity.wxuser.User;
import com.caiheng.api.service.base.impl.BaseServiceImpl;
import com.caiheng.api.service.event.EventPlanTaskService;
import com.caiheng.api.service.titlemenu.TitleMenuService;
import com.caiheng.api.util.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TitleMenuServiceImpl extends BaseServiceImpl<TitleMenuMapper, TitleMenu> implements TitleMenuService {

    @Resource
    private TitleMenuMapper titleMenuMapper;
    @Resource
    private EventPlanTaskService planTaskService;
    @Resource
    private RepairRecordMapper repairRecordMapper;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Value("${web.imgPrefixOss}")
    private String imgPrefixOss;//图片前缀

    @Override
    public List<TitleMenu> selectByState(int state,int is_show_first,int menu_type,String platform) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",state);
            qw.eq("is_show_first",is_show_first);
            qw.eq("menu_type",menu_type);
            qw.orderByAsc("sort");

            String key = "title_menu_" + menu_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    listMenu = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), TitleMenu.class);
                } else {
                    listMenu = titleMenuMapper.selectList(qw);
                    if(listMenu != null && listMenu.size() > 0){
                        for(TitleMenu titleMenu : listMenu){
                            if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                                titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                            }
                        }
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
                }

            }else{
                listMenu = titleMenuMapper.selectList(qw);
                if(listMenu != null && listMenu.size() > 0){
                    for(TitleMenu titleMenu : listMenu){
                        if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                            titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                        }
                    }
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return listMenu;
    }

    @Override
    public List<TitleMenu> selectByStateAndUser(User user, int state, int is_show_first, int menu_type,String platform) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",state);
            qw.eq("is_show_first",is_show_first);
            qw.eq("menu_type",menu_type);
            qw.orderByAsc("sort");
            String key = "title_menu_" + menu_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    listMenu = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), TitleMenu.class);
                } else {
                    listMenu = titleMenuMapper.selectList(qw);
                    if(listMenu != null && listMenu.size() > 0){
                        for(TitleMenu titleMenu : listMenu){
                            if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                                titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                            }
                        }
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
                }

            }else{
                listMenu = titleMenuMapper.selectList(qw);
                if(listMenu != null && listMenu.size() > 0){
                    for(TitleMenu titleMenu : listMenu){
                        if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                            titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                        }
                    }
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

//        List<TitleMenu> listNew = new ArrayList<>();
//        if(CollUtil.isNotEmpty(listMenu)){
//            for (TitleMenu menu : listMenu){
//                if(menu.getId() == 61 && user!=null && user.getIsCloseSign() == 1){
//                    //禁用  显示  签到
//                    TitleMenu titleMenu1 = findById(65);
//                    if(titleMenu1!=null){
//                        if(StrUtil.isNotEmpty(titleMenu1.getLinkLogo())){
//                            titleMenu1.setLinkLogo(imgPrefixOss + titleMenu1.getLinkLogo());
//                        }
//                    }
//                    listNew.add(titleMenu1);
//                }else{
//                    listNew.add(menu);
//                }
//            }
//        }
        return listMenu;
    }

    @Override
    public List<TitleMenu> selectBySpecial(String category_type, User user) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("is_show_first",0);
            qw.eq("menu_type",0);
            qw.eq("category_type",category_type);
            qw.orderByAsc("sort");

            String key = "title_menu_" + category_type;
            boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
            if (blHasKey) {
                Object object = redisTemplate.opsForHash().get(key, key);
                String itemDicts = object != null ? object.toString() : "";
                if (StrUtil.isNotEmpty(itemDicts)) {
                    JSONArray jsonArray = JSONUtil.parseArray(itemDicts);
                    listMenu = JSONUtil.toList(JSONUtil.toJsonStr(jsonArray), TitleMenu.class);
                } else {
                    listMenu = titleMenuMapper.selectList(qw);
                    if(listMenu != null && listMenu.size() > 0){
                        for(TitleMenu titleMenu : listMenu){
                            if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                                titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                            }
                        }
                    }
                    redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
                }

            }else{
                listMenu = titleMenuMapper.selectList(qw);
                if(listMenu != null && listMenu.size() > 0){
                    for(TitleMenu titleMenu : listMenu){
                        if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                            titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                        }
                    }
                }
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        if(user != null){
            for (TitleMenu menu : listMenu){
                int eventType = 0;
                if(menu.getTitleName().equals("巡检")){
                    eventType = 1;
                } else if(menu.getTitleName().equals("巡更")){
                    eventType = 2;
                } else if(menu.getTitleName().equals("保洁")){
                    eventType = 3;
                } else if(menu.getTitleName().equals("消杀")){
                    eventType = 5;
                } else if(menu.getTitleName().equals("维保")){
                    eventType = 6;
                }
                if(eventType > 0){
                    Map<String, Object> mapParam = new HashMap<>();
                    mapParam.put("sys_user_id", user.getSysUserId());
                    mapParam.put("state", 0);
                    mapParam.put("event_type", eventType);
//                    mapParam.put("beginTime", DateUtil.getMonthFirstDayStr());
//                    mapParam.put("endTime", DateUtil.getMonthLastDayStr());

                    mapParam.put("beginTime", DateUtil.getTodayZero());
                    mapParam.put("endTime", DateUtil.getTodayLast());
                    long count = planTaskService.getTaskCount(mapParam);
                    menu.setBadge(count);
                }

                if(menu.getTitleName().equals("业主报事")){
                    QueryWrapper<RepairRecord> qw = new QueryWrapper<>();
                    qw.eq("state",1);
                    qw.eq("op_wx_user_id",user.getId());
                    long count = repairRecordMapper.selectCount(qw);
                    menu.setBadge(count);

                } else if(menu.getTitleName().equals("抢单中心")){
                    QueryWrapper<RepairRecord> qw = new QueryWrapper<>();
                    qw.eq("state", 0);
                    qw.eq("estate_id", user.getEstateId());
                    qw.orderByDesc("create_time");

                    long count = repairRecordMapper.selectCount(qw);
                    menu.setBadge(count);
                }
            }
        }


        return listMenu;
    }

    @Override
    public void resetMenuData(int menu_type) {
        List<TitleMenu> listMenu = new ArrayList<>();
        try{
            QueryWrapper<TitleMenu> qw = new QueryWrapper<>();
            qw.eq("state",0);
            qw.eq("is_show_first",1);
            qw.eq("menu_type",menu_type);
            qw.orderByAsc("sort");

            String key = "title_menu_" + menu_type;

            listMenu = titleMenuMapper.selectList(qw);
            if(listMenu != null && listMenu.size() > 0){
                for(TitleMenu titleMenu : listMenu){
                    if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                        titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                    }
                }
            }
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listMenu));
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void resetMenuDataSpecial(String service_type) {
        QueryWrapper<TitleMenu> qwSpecial = new QueryWrapper<>();
        qwSpecial.eq("state",0);
        qwSpecial.eq("menu_type",0);
        qwSpecial.eq("category_type",service_type);
        qwSpecial.orderByAsc("sort");

        String key = "title_menu_" + service_type;

        List<TitleMenu> listSpecialMenu = titleMenuMapper.selectList(qwSpecial);
        if(CollUtil.isNotEmpty(listSpecialMenu)){
            for(TitleMenu titleMenu : listSpecialMenu){
                if(StrUtil.isNotEmpty(titleMenu.getLinkLogo())){
                    titleMenu.setLinkLogo(imgPrefixOss + titleMenu.getLinkLogo());
                }
            }
        }
        redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(listSpecialMenu));
    }
}
